package com.zh.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zh.common.QueryPageParam;
import com.zh.common.Result;
import com.zh.entity.User;
import com.zh.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @author zhouhang
 * @date 2025/04/01
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/register")
    public Result register(@RequestBody User user) {
        return userService.register(user);
    }

    @PostMapping("/login")
    public Result login(@RequestBody User user, HttpSession session) {
        return userService.login(user, session);
    }

    @PostMapping("/logout")
    public Result logout(HttpSession session) {
        return userService.logout(session);
    }


    @PostMapping("/save")
    public boolean save(@RequestBody User user) {
        return userService.save(user);
    }

    @PutMapping("/update")
    public boolean update(@RequestBody User user) {
        return userService.updateById(user);
    }

    @DeleteMapping("/delete")
    public boolean delete(@RequestParam Integer id) {
        return userService.removeById(id);
    }

    @GetMapping("/search")
    public Result search(@RequestBody User user) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(User::getName, user.getName());
        return Result.success(userService.list(wrapper), 0L);
    }

    /**
     * 分页查询
     * wrapper是负责添加查询条件的 无参时 ==> wrapper无任何条件 ==> 查询全部
     * IPage 是分页结果
     * 封装了分页结果，total是总记录数，records是分页结果
     * 封装了分页参数，pageNum是当前页，pageSize是每页记录数
     */
    @PostMapping("/listPage")
    public Result listPage(@RequestBody QueryPageParam queryPageParam) {
        return userService.listPage(queryPageParam);
    }


}
